local app = {}

function app.add_text(item)
	app.view[1].content[1].text = app.view[1].content[1].text..item.content[1].text
end

function app.del()
	app.view[1].content[1].text = ''
end

function app.compute()
	local f = loadstring("return "..app.view[1].content[1].text)

	if f ~= nil then result = f() else app.view[1].content[1].text = 'Error' end
	
	if result ~= nil then
		app.view[1].content[1].text = result
		result = nil
	end
end

app.view = {
	{name='row', color={0.8, 0.8, 0.8}, size = 0.4, content = {
		{name='label', text='', color = {0.1, 0.1, 0.1}}
	}},
	{name='row', color={0.25, 0.25, 0.25}, size = 0.6, content = {
		{name='column', color={0.25, 0.25, 0.25}, size = 0.8, content = {
			{name='row', color={0.25, 0.25, 10.60}, size = 0.25, content = {
				{name='column', color={0.25, 0.25, 0.25}, size = 1/3, content = {
					{name='button', color={0.25, 0.25, 0.25}, func=app.add_text, padding=5, radius=15, content = {{name='label', text='7', color={1,1,1}}}},
				}},

				{name='column', color={0.25, 0.25, 0.25}, size = 1/3, content = {
					{name='button', color={0.25, 0.25, 0.25}, func=app.add_text, padding=5, radius=15, content = {{name='label', text='8', color={1,1,1}}}},
				}},

				{name='column', color={0.25, 0.25, 0.25}, size = 1/3, content = {
					{name='button', color={0.25, 0.25, 0.25}, func=app.add_text, padding=5, radius=15, content = {{name='label', text='9', color={1,1,1}}}},
				}},
			}},
			{name='row', color={0.25, 0.25, 0.25}, size = 0.25, content = {
				{name='column', color={0.25, 0.25, 0.25}, size = 1/3, content = {
					{name='button', color={0.25, 0.25, 0.25}, func=app.add_text, padding=5, radius=15, content = {{name='label', text='4', color={1,1,1}}}},
				}},

				{name='column', color={0.25, 0.25, 0.25}, size = 1/3, content = {
					{name='button', color={0.25, 0.25, 0.25}, func=app.add_text, padding=5, radius=15, content = {{name='label', text='5', color={1,1,1}}}},
				}},

				{name='column', color={0.25, 0.25, 0.25}, size = 1/3, content = {
					{name='button', color={0.25, 0.25, 0.25}, func=app.add_text, padding=5, radius=15, content = {{name='label', text='6', color={1,1,1}}}},
				}},
			}},
			{name='row', color={0.25, 0.25, 0.25}, size = 0.25, content = {
				{name='column', color={0.25, 0.25, 0.25}, size = 1/3, content = {
					{name='button', color={0.25, 0.25, 0.25}, func=app.add_text, padding=5, radius=15, content = {{name='label', text='1', color={1,1,1}}}},
				}},

				{name='column', color={0.25, 0.25, 0.25}, size = 1/3, content = {
					{name='button', color={0.25, 0.25, 0.25}, func=app.add_text, padding=5, radius=15, content = {{name='label', text='2', color={1,1,1}}}},
				}},

				{name='column', color={0.25, 0.25, 0.25}, size = 1/3, content = {
					{name='button', color={0.25, 0.25, 0.25}, func=app.add_text, padding=5, radius=15, content = {{name='label', text='3', color={1,1,1}}}},
				}},
			}},
			{name='row', color={0.25, 0.25, 0.25}, size = 0.25, content = {
				{name='column', color={0.25, 0.25, 0.25}, size = 1/3, content = {
					{name='button', color={0.25, 0.25, 0.25}, func=app.add_text, padding=5, radius=15, content = {{name='label', text='.', color={1,1,1}}}},
				}},

				{name='column', color={0.25, 0.25, 0.25}, size = 1/3, content = {
					{name='button', color={0.25, 0.25, 0.25}, func=app.add_text, padding=5, radius=15, content = {{name='label', text='0', color={1,1,1}}}},
				}},

				{name='column', color={0.25, 0.25, 0.25}, size = 1/3, content = {
					{name='button', color={0.25, 0.25, 0.25}, func=app.compute, padding=5, radius=15, content = {{name='label', text='=', color={1,1,1}}}},
				}},
			}},
		}},
		{name='column', color={0.38, 0.38, 0.38}, size = 0.2, content = {
			{name='row', color={0.38, 0.38, 0.38}, size = 0.2, content = {
				{name='button', color={0.38, 0.38, 0.38}, func=app.del, padding=5, radius=15, content = {{name='label', text='DEL', color={1,1,1}}}},
			}},
			{name='row', color={0.38, 0.38, 0.38}, size = 0.2, content = {
				{name='button', color={0.38, 0.38, 0.38}, func=app.add_text, padding=5, radius=15, content = {{name='label', text='/', color={1,1,1}}}},
			}},
			{name='row', color={0.38, 0.38, 0.38}, size = 0.2, content = {
				{name='button', color={0.38, 0.38, 0.38}, func=app.add_text, padding=5, radius=15, content = {{name='label', text='*', color={1,1,1}}}},
			}},
			{name='row', color={0.38, 0.38, 0.38}, size = 0.2, content = {
				{name='button', color={0.38, 0.38, 0.38}, func=app.add_text, padding=5, radius=15, content = {{name='label', text='-', color={1,1,1}}}},
			}},
			{name='row', color={0.38, 0.38, 0.38}, size = 0.2, content = {
				{name='button', color={0.38, 0.38, 0.38}, func=app.add_text, padding=5, radius=15, content = {{name='label', text='+', color={1,1,1}}}},
			}},
		}},
	}},
}

function app.update()
	return app.view
end

return app